int GetOFN(char *szOpenName, const char *szFilter) {
	char szNameTemp[128]="\0"; 

	OPENFILENAME ofn;
	memset(&ofn,0,sizeof(OPENFILENAME));
	ofn.lStructSize =		sizeof(OPENFILENAME);
	ofn.hwndOwner=			hMainDlg;
	ofn.lpstrInitialDir =	szFileName;
	ofn.lpstrFilter =		szFilter;
	ofn.lpstrFile =			szNameTemp;
	ofn.nMaxFile =			0x7ff;
	ofn.lpstrTitle =		"Open File";
	ofn.Flags =				OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
	if(!GetOpenFileName(&ofn)) return 0; //if error or cancel, bail

	strcpy(szOpenName, szNameTemp);
	return 1;
}

int GetSFN(char *szSaveName, const char *szFilter) {
	char szNameTemp[128], *szExt;
	strcpy(szNameTemp,szFileName);
	szExt=strrchr(szNameTemp,'.');
	if(szExt) *szExt='\0';
		
	OPENFILENAME ofn;
	memset(&ofn, 0, sizeof(OPENFILENAME));
	ofn.lStructSize =		sizeof(OPENFILENAME);
	ofn.hwndOwner=			hMainDlg;
	ofn.lpstrFilter =		szFilter;
	ofn.lpstrFile =			szNameTemp;
	ofn.nMaxFile =			0x7ff;
	ofn.lpstrTitle =		"Save File";
	ofn.Flags =				OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT;
	if(!GetSaveFileName(&ofn)) return 0; //if error or cancel, bail
		
	strcpy(szSaveName,szNameTemp);
	return ofn.nFilterIndex;
}

void SetBaseName() {
	if(szFileName[0]!='\0') 
		szBaseName=strrchr(szFileName, '\\')+1; 
	else szBaseName=NULL;	
}

void EnableMenu(int iState) {
	EnableMenuItem(hMainMenu,IDM_FILE_EXP_TEXT,MF_BYCOMMAND | iState);
}

DWORD WINAPI LoadFile(PVOID pvVoid) {
	EnableMenu(MF_GRAYED);

	int iError=rrRawFile.Open((const char*)szFileName);
	if(iError) ErrorMessage(hMainDlg,iError);
	
	else  {
		//rrRawFile.BlockSet(0);
		
		//set titlebar text
		SetBaseName();
		sprintf(szTitle,"%s - %s",PROG_TITLE,szBaseName);
		SetWindowText(hMainDlg,szTitle);
		
		EnableMenu(MF_ENABLED);

		//set edit fields
		SendDlgItemMessage(hMainDlg,IDC_BLOCK_SPIN,UDM_SETRANGE,1,rrRawFile.iBlocks);
		SetDlgItemInt(hMainDlg,IDC_BLOCK_EDIT,rrRawFile.iCurBlock+1,0);
		SetDlgItemInt(hMainDlg,IDC_BLOCKS_EDIT,rrRawFile.iBlocks,0);
		SetDlgItemInt(hMainDlg,IDC_HEIGHT_EDIT,1,FALSE);
		SetDlgItemInt(hMainDlg,IDC_ZOOM_EDIT,1,FALSE);

		//set file length
		sprintf(szMessage,"%.3f s",rrRawFile.dFileLen);
		SetDlgItemText(hMainDlg,IDC_FILE_TIME_EDIT,szMessage);
		
		SetDlgInfo();
	}

	return 0;
}

DWORD WINAPI ProgressUpdate() {

	/*if(!pProgress.ulDone) { //update progress info
		pProgress.Set();
		if(hProgDlg) SendMessage(hProgDlg,UDM_UPDATE,0,0);
	}*/
	
	//if(!pProgress.iWorking && hProgDlg) SendMessage(hProgDlg,WM_CLOSE,0,0);

	if(hProgDlg) SendMessage(hProgDlg,UDM_UPDATE,0,0);
	
	return 0;
}

int MakeFolder(char *szFolderName) {
	SECURITY_ATTRIBUTES saFolderSec;
	saFolderSec.nLength=sizeof(SECURITY_ATTRIBUTES);
	saFolderSec.bInheritHandle=true;
	saFolderSec.lpSecurityDescriptor=NULL;		
		
	strcpy(szFolderName,szFileName); //copy RAW file name
	char *szExt=strrchr(szFolderName,'.'); //remove extension
	if(szExt) *szExt='\0';

	if(CreateDirectory(szFolderName,&saFolderSec)==ERROR_PATH_NOT_FOUND)
		return -1;
	return 0;
}

void OpenWith(char *szFileName) {
	SHELLEXECUTEINFO seiShellExec;

	memset(&seiShellExec,0,sizeof(SHELLEXECUTEINFO));
	seiShellExec.cbSize=sizeof(SHELLEXECUTEINFO);
	seiShellExec.lpFile=szFileName;
	seiShellExec.nShow=SW_SHOWNORMAL;
	ShellExecuteEx(&seiShellExec);
}

DWORD WINAPI ExportFile(PVOID pvVoid) {
	int iError;
	pProgress.Set(0,ulSelRange);
	pProgress.iWorking=true;

	char szFolderName[1024], szTextName[1024];
		
	//create directory for exports
	if(MakeFolder(szFolderName)) return 0;

	//create text file
	for(int iPol=0; iPol<2 && pProgress.iWorking; iPol++) {
		sprintf(szTextName,"%s\\B%03i_C%02i_P%i.txt",szFolderName,rrRawFile.iCurBlock+1,iSelChan+1,iPol+1);
		
		if(iError=rrRawFile.ExportText(szTextName,iSelChan,iPol,ulSelTime,ulSelRange,pProgress.szTitle,pProgress.ulDone,pProgress.iWorking,ProgressUpdate))
			ErrorMessage(hMainDlg,iError);
	}	

	//OpenWith(szTextName); //shell execute
		
	pProgress.iWorking=false;
	
	return 0;
}

LRESULT CALLBACK ProgressProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam) {	
	switch(iMsg) {
		case WM_COMMAND: {
			switch(LOWORD(wParam)) {
				case IDCANCEL: SendMessage(hWnd,WM_CLOSE,0,0); return 0;
			}
			return 0;
		}

		case WM_INITDIALOG:	
			hProgDlg=hWnd;			
			SendDlgItemMessage(hWnd,IDC_PROG_BAR,PBM_SETRANGE32,0,pProgress.ulTotal);
			SendDlgItemMessage(hWnd,IDC_PROG_BAR,PBM_SETSTEP,MAKEWPARAM(1,0),0);

		case UDM_UPDATE:
			SetWindowText(hWnd,pProgress.szTitle);
			pProgress.Set();
			SetDlgItemText(hWnd,IDC_PROG_TEXT,pProgress.szText);
			SendDlgItemMessage(hWnd,IDC_PROG_BAR,PBM_SETPOS,pProgress.ulDone,0);
			return 0;
 
		case WM_CLOSE:
			pProgress.iWorking=false;
			hProgDlg=NULL;
			EndDialog(hWnd,0); 
			return 0;
	}

	return 0;
}
